//
//  OMGoodDetailBottomBar.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/23.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodDetailBottomBar.h"
#import "OMProduct.h"
@interface OMGoodDetailBottomBar()
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *cartButton;
@property (nonatomic, strong) UIButton *badgeView;

@end

@implementation OMGoodDetailBottomBar

- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)configure
{
    self.backgroundColor = SM_whiteColor;

    UIView *line = [UIView new];
    line.backgroundColor = SM_naviShadowColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@.5f);
    }];
    [_cartButton setTitle:@"" forState:UIControlStateNormal];


    _cartButton = [UIButton new];
    [_cartButton setImage:[UIImage imageNamed:@"cart_btn_50x50_"] forState:UIControlStateNormal];
    [_cartButton addTarget:self action:@selector(cartButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cartButton];
    [_cartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.centerY.equalTo(self.mas_centerY);
        make.width.height.equalTo(@(SM_SIZE(100)));
    }];

    //购物车
    _button = [UIButton new];
    [_button setBackgroundImage:[UIImage imageWithColor:SM_sangouColor] forState:UIControlStateNormal];
    [_button setBackgroundImage:[UIImage imageWithColor:SM_disableColor] forState:UIControlStateDisabled];
    [_button.titleLabel setFont:SM_CFont(33)];
    [_button  setTitleColor:SM_blackColor forState:UIControlStateNormal];
    [_button setTitleColor:SM_whiteColor forState:UIControlStateDisabled];
    [_button setTitle:@"加入购物车" forState:UIControlStateNormal];
    [_button setTitle:@"库存不足" forState:UIControlStateDisabled];
    [_button.layer setCornerRadius:SM_SIZE(10)];
    [_button setClipsToBounds:YES];
    [_button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button];
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(@(SM_SIZE(20)));
        make.bottom.equalTo(@(SM_SIZE(-20)));
        make.right.equalTo(_cartButton.mas_left).offset(SM_SIZE(-30));
    }];
    [_cartButton addSubview:self.badgeView];
    [_badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_cartButton.mas_top).offset(SM_SIZE(30));
        make.left.equalTo(_cartButton.mas_right).offset(SM_SIZE(-40));
        make.width.mas_greaterThanOrEqualTo(SM_SIZE(40));
        make.height.equalTo(@(SM_SIZE(40)));
    }];


    //购物车数量
    @weakify(self);
    [SMNotificationMonitorRACSignal(OMSHOPPINGCART_BADAGE_NUM_CHANGE_NOTIFICATION) subscribeNext:^(NSNotification *notification) {
        @strongify(self);
        [self setBadge:[notification.object unsignedIntegerValue]];
    }];

    [self setBadge:KTSystemStorageSingleton.numOfCartItem];
}

#pragma mark - Actions
- (void)buttonTap:(UIButton *)sender
{
    [OMFMDBCartHelper itemAddition:self.product
               withButtonAnimation:sender
                        targetView:self.badgeView];
}


- (void)setProduct:(OMProduct *)product
{
    _product = product;
    self.button.enabled = _product.availableStock;
}


- (UIButton *)badgeView
{
    if (!_badgeView) {
        _badgeView = [UIButton new];
        _badgeView.userInteractionEnabled = NO;
        _badgeView.backgroundColor = SM_dotRedColor;
        _badgeView.titleLabel.font = SM_NFont(25);
        [_badgeView setTitleColor:SM_whiteColor forState:UIControlStateNormal];
        _badgeView.layer.cornerRadius = SM_SIZE(40)/2.f;
        _badgeView.clipsToBounds = YES;
        _badgeView.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(15), SM_SIZE(15), SM_SIZE(15), SM_SIZE(15));
        _badgeView.hidden = YES;
    }
    return _badgeView;
}

- (void)setBadge:(NSUInteger)number
{
    _badgeView.hidden = !number;
    [_badgeView setTitle:[@(number) stringValue] forState:UIControlStateNormal];
}

- (void)cartButtonTap:(id)sender
{
    NaviPush(@"OMShoppingCartViewController", nil);
}
@end
